package com.kushe.util.tools;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * Cookies操作方法
 *
 * Created by WhiteBlue on 15/4/4.
 */
public class CookiesUtil {
    //从cookies中获得参数
    public static Cookie getCookieObject(String name,HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if(cookies != null) {
            Cookie[] arr$ = cookies;
            int len$ = cookies.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                Cookie cookie = arr$[i$];
                if(cookie.getName().equals(name)) {
                    return cookie;
                }
            }
        }
        return null;
    }

    public static void removeCookie(HttpServletResponse response,String name){
        setCookie(response,name, (String)null, 0, "/", (String)null);
    }

    public static  void setCookie(HttpServletResponse response,String name, String value, int maxAgeInSeconds, String path, String domain) {
        Cookie cookie = new Cookie(name, value);
        if(domain != null) {
            cookie.setDomain(domain);
        }
        cookie.setMaxAge(maxAgeInSeconds);
        cookie.setPath(path);
        response.addCookie(cookie);
    }
}
